package com.ruoyi.app.service.impl;

import com.ruoyi.app.domain.*;
import com.ruoyi.app.service.impl.mapper.OperFaultGroupMapper;
import com.ruoyi.app.service.impl.mapper.OperFaultMapper;
import com.ruoyi.app.service.impl.mapper.TaskApplyMapper;
import com.ruoyi.app.service.ITaskApplyService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ITaskApplyServiceImpl implements ITaskApplyService {

    @Autowired
    private TaskApplyMapper taskApplyMapper;
    @Autowired
    private OperFaultMapper operFaultMapper;
    @Autowired
    private OperFaultGroupMapper operFaultGroupMapper;
    /**
     * 获取设备列表
     * @param equip
     * @return
     */
    @Override
    public List<Equip> selectEquipList(Equip equip) {

        return taskApplyMapper.selectEquipList(equip);
    }
    /**
     * 获取线路列表
     * @param circuit
     * @return
     */
    @Override
    public List<Circuit> selectCircuitList(Circuit circuit) {
        return taskApplyMapper.selectCircuitList(circuit);
    }
    /**
     * 获取区域列表
     * @param region
     * @return
     */
    @Override
    public List<Region> selectReginList(Region region) {
        return taskApplyMapper.selectRegionList(region);
    }

    /**
     * 查询任务
     * @param taskApply
     * @return
     */
    @Override
    public List<TaskApply> selectTaskApplyList(TaskApply taskApply) {
        return  taskApplyMapper.selectTaskApplyList(taskApply);
    }

    /**
     * 插入任务
     * @param taskApply
     * @return
     */
    @Override
    public int insertTaskApply(TaskApply taskApply) {

        int res =  taskApplyMapper.insertTaskApply(taskApply);
        if(res == 1){
            OperFaultGroup operFaultGroup = new OperFaultGroup();
            operFaultGroup.setStatus(0);
            operFaultGroup.setOccurTime(DateUtils.getTime());
            int id = operFaultGroupMapper.insertOperFaultGroup(operFaultGroup);
            if(id > 0){
                String[] regions = taskApply.getRegionId().split(",");
                for (String region:
                regions) {

                    //普通断路器
                    if(!taskApply.getEquipId().equals("")){
                        String [] equips = taskApply.getEquipId().split(",");
                        for (String eq:
                        equips) {
                            OperFault operFault = new OperFault();
                            operFault.setFaultGroupId(operFaultGroup.getId());
                            operFault.setFdNo(Integer.toUnsignedLong(taskApply.getCircuitId()));
                            operFault.setSwNo(Long.parseLong(eq));
                            operFault.setSwType(1L);
                            operFault.setOccurTime(DateUtils.getTime());
                            Circuit circuit = taskApplyMapper.selectCircuitById(Integer.toUnsignedLong(taskApply.getCircuitId()));
                            if(circuit != null){
                                operFault.setFdName(circuit.getCircuitName());
                            }
                            Equip equip = taskApplyMapper.selectEquipById(Long.parseLong(eq));
                            if(equip != null){
                                operFault.setSwName(equip.getEquipName());
                            }

                            operFault.setRegionId(Long.parseLong(region));
                            operFaultMapper.insertOperFault(operFault);
                        }

                    }
                    //发电低压断路器
                    if(!taskApply.getElectricEquips().equals("")){
                        String [] electrics = taskApply.getElectricEquips().split(",");
                        for (String ele:
                                electrics) {
                            OperFault operFault = new OperFault();
                            operFault.setFaultGroupId(operFaultGroup.getId());
                            operFault.setFdNo(Integer.toUnsignedLong(taskApply.getCircuitId()));
                            operFault.setSwNo(Long.parseLong(ele));
                            operFault.setSwType(2L);
                            operFault.setOccurTime(DateUtils.getTime());
                            Circuit circuit = taskApplyMapper.selectCircuitById(Integer.toUnsignedLong(taskApply.getCircuitId()));
                            if(circuit != null){
                                operFault.setFdName(circuit.getCircuitName());
                            }
                            Equip equip = taskApplyMapper.selectEquipById(Long.parseLong(ele));
                            if(equip != null){
                                operFault.setSwName(equip.getEquipName());
                            }

                            operFault.setRegionId(Long.parseLong(region));
                            operFaultMapper.insertOperFault(operFault);
                        }
                    }
                    //储能低压断路器
                    if(!taskApply.getEnergyEquips().equals("")){
                        String [] energys = taskApply.getEnergyEquips().split(",");
                        for (String ene:
                                energys) {
                            OperFault operFault = new OperFault();
                            operFault.setFaultGroupId(operFaultGroup.getId());
                            operFault.setFdNo(Integer.toUnsignedLong(taskApply.getCircuitId()));
                            operFault.setSwNo(Long.parseLong(ene));
                            operFault.setSwType(3L);
                            operFault.setOccurTime(DateUtils.getTime());
                            Circuit circuit = taskApplyMapper.selectCircuitById(Integer.toUnsignedLong(taskApply.getCircuitId()));
                            if(circuit != null){
                                operFault.setFdName(circuit.getCircuitName());
                            }
                            Equip equip = taskApplyMapper.selectEquipById(Long.parseLong(ene));
                            if(equip != null){
                                operFault.setSwName(equip.getEquipName());
                            }

                            operFault.setRegionId(Long.parseLong(region));
                            operFaultMapper.insertOperFault(operFault);
                        }
                    }
                }
            }
        }
        return res;
    }

    /**
     * 更新任务
     * @param taskApply
     * @return
     */
    @Override
    public int updateTaskApply(TaskApply taskApply) {
        return taskApplyMapper.updateTaskApply(taskApply);
    }
}
